<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <script type="text/javascript" src="/js/jquery-1.7.2.min.js"></script>
        <link rel="stylesheet" href="stylesheets/qunit.css" type="text/css" media="screen" />
        <script type="text/javascript" src="/js/qunit.js"></script>
        
        <script type="text/javascript" src="/js/blanket.js"
            data-cover-flags="ignoreError"></script>
        
        <script type="text/javascript" src="/js/protobuf.js"></script>
        <script type="text/javascript" src="/js/pbj.js"></script>
        <script type="text/javascript" src="/js/googleProtocolBufferMessagesReader.js" data-cover></script>
        <script type="text/javascript" src="/js/googleProtocolBufferMessagesAjaxHelper.js" data-cover></script>
        <script type="text/javascript" src="/js/examplemessage.proto.js"></script>

        <!-- from http://miskun.com/javascript/internet-explorer-and-binary-files-data-access/ -->
        <!--[if IE]>
            <script type="text/vbscript">
                Function IEBinaryToArray_ByteStr(Binary)
                    IEBinaryToArray_ByteStr = CStr(Binary)
                End Function
                Function IEBinaryToArray_ByteStr_Last(Binary)
                    Dim lastIndex
                    lastIndex = LenB(Binary)
                    if lastIndex mod 2 Then
                        IEBinaryToArray_ByteStr_Last = Chr( AscB( MidB( Binary, lastIndex, 1 ) ) )
                    Else
                        IEBinaryToArray_ByteStr_Last = ""
                    End If
                End Function
            </script>

        <![endif]-->
        
        <!--[if IE 10]>
            <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <![endif]-->

        <script>
            var gpburl = "http://localhost:8080/gpb"; /*change to your domain name or IP address and ports*/
            var retryCount = 0;
            var timeoutInMillis = 10000;

            function printComment(testNumber, comment) {
                writeToElement.innerHTML = writeToElement.innerHTML
                    + "<br/>(" + testNumber + "), "
                    + "<e style='color:grey;'>" + comment + "</e>";
            }
            function printPass(testNumber, testDesc) {
                writeToElement.innerHTML = writeToElement.innerHTML
                    + "<br/>(" + testNumber + "), "
                    + "<b style='color:green;'>passed</b> " + testDesc;
            }
            function printFail(testNumber, testDesc, msg) {
                writeToElement.innerHTML = writeToElement.innerHTML
                    + "<br/>(" + testNumber + "), "
                    + "<b style='color:red;'>failed</b> " + testDesc
                    + "<b style='color:red;'>; ERROR:" + msg + "</b>";
            }


            var createYourPROTOMessage = function() {
                return new com.climbwithyourfeet.proto.ExampleMsg;
            };

            var utf8Text = 'ct='+ window.encodeURIComponent('text/plain') + '&ec=' + window.encodeURIComponent('UTF-8');
            var utf7Text = 'ct='+ window.encodeURIComponent('text/plain') + '&ec=' + window.encodeURIComponent('UTF-7');
            var iso88591Text = 'ct='+ window.encodeURIComponent('text/plain') + '&ec=' + window.encodeURIComponent('ISO-8859-1');
            var windows1252Text = 'ct='+ window.encodeURIComponent('text/plain') + '&ec=' + window.encodeURIComponent('Windows-1252');
            var utf8OctetStream = 'ct='+ window.encodeURIComponent('application/octet-stream') + '&ec=' + window.encodeURIComponent('UTF-8');

            $(document).ready(function() {

                var testNumber = 0;

                writeToElement.innerHTML = "<br/><b>[" + navigator.userAgent + "]</b>";

                /* functions to invoke requests with variations on the ajax method, the javascript data type,
                 * the server content type and server character encoding */

                function req3(testDesc, url, fnc) {
                    testNumber++;
                    expect( 1 );

                    var printCallback = function(msg) {
                        printComment(testNumber, msg);
                    }

                    var successCallback = function(messages) {
                        start();
                        var pass = (messages != undefined);
                        pass = pass && (messages.length > 0);
                        ok(pass, 'request and deserialization worked');
                        if (pass) {
                            printCallback('last message=' + messages[messages.length-1]);
                            printPass(testNumber, testDesc);
                        } else {
                            printFail(testNumber, testDesc, 'deserialized msg missing properties');
                        }
                    }
                    var errorCallback = function(msg) {
                        start();
                        ok(false, 'failed for request and parse');
                        printFail(testNumber, testDesc, msg);
                    }

                    var m = [];
                    var perMessageCallback = function renderEvent(gpbMessage, dictionary) {
                        m.push(gpbMessage);
                    };
                    var completedCallback = function deserializationIsDone(dictionary) {
                        printCallback('Completed.  passing ' + m.length + ' messages');
                        successCallback(m);
                    };
                    var errorHandle = function(errorString, dictionary) {
                        errorCallback(errorString);
                    };
                    var userdictionary = {'key': "key"};

                    try {
                        fnc(url, createYourPROTOMessage, perMessageCallback, 
                            completedCallback, errorHandle, userdictionary, timeoutInMillis);
                    } catch(e) {
                        errorCallback(e.message);
                    }
                }

                // chrome, webkit, and mozilla handle utf8 octet-stream
                var desc1 = 'XMLHttpRequest + ArrayBuffer + TypedArray + (w/ delimiters) + utf8 + octet-stream';
                asyncTest(desc1, function() {
                    req3(desc1, gpburl + '?' + utf8OctetStream, makeXMLHttpRequestForArrayBufferWithTypedArray)});

                var desc2 = 'XMLHttpRequest + ArrayBuffer + TypedArray + (w/ delimiters, w/unicode chars) + utf8 + octet-stream';
                asyncTest(desc2, function() {
                    req3(desc2, gpburl + '?' + utf8OctetStream + '&u=true', makeXMLHttpRequestForArrayBufferWithTypedArray)});
                
                var desc3 = 'XMLHttpRequest + (w/ delimiters) + utf8 + octet-stream';
                asyncTest(desc3, function() {
                    req3(desc3, gpburl + '?' + utf8OctetStream, makeXMLHttpRequest)});

                var desc4 = 'XMLHttpRequest + (w/ delimiters, w/unicode chars) + utf8 + octet-stream';
                asyncTest(desc4, function() {
                    req3(desc4, gpburl + '?' + utf8OctetStream + '&u=true', makeXMLHttpRequest)});
                
                var desc5 = 'XMLHttpRequest + synchronous +  (w/ delimiters) + utf8 + octet-stream';
                asyncTest(desc5, function() {
                    req3(desc5, gpburl + '?' + utf8OctetStream, makeSynchronousXMLHttpRequest)});
                
                var desc6 = 'XMLHttpRequest + synchronous +  (w/ delimiters, w/unicode chars) + utf8 + octet-stream';
                asyncTest(desc6, function() {
                    req3(desc6, gpburl + '?' + utf8OctetStream + '&u=true', makeSynchronousXMLHttpRequest)});
                

                //utf7 + text and XDomainRequest works for ie9
                var desc7 = 'XDomainRequest + (w/ delimiters) + utf7 + text';
                asyncTest(desc7, function() {
                    req3(desc7, gpburl + '?' + utf7Text, makeXDomainRequest)});
                
                var desc8 = 'XDomainRequest + (w/ delimiters, w/unicode chars beware!) + utf7 + text';
                asyncTest(desc8, function() {
                    req3(desc8, gpburl + '?' + utf7Text + '&u=true', makeXDomainRequest)});
                
                var desc9 = 'XDomainRequest + (w/ delimiters) + utf8 + octet-stream';
                asyncTest(desc9, function() {
                    req3(desc9, gpburl + '?' + utf8OctetStream, makeXDomainRequest)});
                
                var desc10 = 'XDomainRequest + (w/ delimiters, w/unicode chars) + utf8 + octet-stream';
                asyncTest(desc10, function() {
                    req3(desc10, gpburl + '?' + utf8OctetStream + '&u=true', makeXDomainRequest)});
                
                var desc11 = 'XDomainRequest + (w/ delimiters) + Windows-1252 + text';
                asyncTest(desc11, function() {
                    req3(desc11, gpburl + '?' + windows1252Text, makeXDomainRequest)});
                
                var desc12 = 'XDomainRequest + (w/ delimiters, w/unicode chars) + Windows-1252 + text';
                asyncTest(desc12, function() {
                    req3(desc12, gpburl + '?' + windows1252Text + '&u=true', makeXDomainRequest)});


                //utf8 + octet-stream and ActiveXObjectRequest works for ie6-ie8 
                var desc13 = 'ActiveXObjectRequest + (w/ delimiters) + utf8 + octet-stream';
                asyncTest(desc13, function() {
                    req3(desc13, gpburl + '?' + utf8OctetStream, makeBinaryActiveXObjectRequest)});
                
                var desc14 = 'ActiveXObjectRequest + (w/ delimiters, w/unicode chars) + utf8 + octet-stream';
                asyncTest(desc14, function() {
                    req3(desc14, gpburl + '?' + utf8OctetStream + '&u=true', makeBinaryActiveXObjectRequest)});
                
                // works for ie8 ?
                var desc15 = 'ActiveXObjectRequest + (w/ delimiters) + Windows-1252 + text';
                asyncTest(desc15, function() {
                    req3(desc15, gpburl + '?' + windows1252Text, makeBinaryActiveXObjectRequest)});
                
                //  check for unicode characters
                var desc16 = 'ActiveXObjectRequest + (w/ delimiters, w/unicode chars) + Windows-1252 + text';
                asyncTest(desc16, function() {
                    req3(desc16, gpburl + '?' + windows1252Text + '&u=true', makeBinaryActiveXObjectRequest)});
                
            });
        </script>
    </head>
    <body>
        <h1 id="qunit-header">QUnit test for Javascript Google Protocol Buffer libraries</h1>
        <h2 id="qunit-banner"></h2>
        <div id="qunit-testrunner-toolbar"></div>
        <h2 id="qunit-userAgent"></h2>
        <ol id="qunit-tests"></ol>
        <div id="qunit-fixture">test markup, will be hidden</div>
        <div id="output"></div>
        <br/>

        <script type='text/javascript'>
            writeToElement = document.getElementById('output');
        </script>
    </body>
</html>
